const spiralOrder = matrix => {
    const res = [];
    const [m, n] = [matrix.length, matrix[0]?.length];
    console.log(m, n);
    if (!m || !n) {
        return res
    }

    // 初始化 左 右  上  下的值， 动态 
    let [left, right, up, down] = [0, n - 1, 0, m - 1];
    // [0, 0]  i 行 j 列
    while(1) {
        // 向右
        for (let j = left; j <= right; j++) {
            res.push(matrix[up][j]);
        }
        up++;
        if (up > down) break;

        // 向下   j不变  right  
        for (let i = up; i <=down; i++) {
            res.push(matrix[i][right]);
        }
        right--;
        if (right < left) break;
        // 向左
        // i 不变  j-- down
        for (let j = right; j >= left; j--) {
            res.push(matrix[down][j]);
        }
        down--;
        if (down < up) break;
        // 向上
        // j 不变   i--  left
        for (let i = down; i >= up; i--) {
            res.push(matrix[i][left]);
        }
        left++;
        if (left > right) break;
    }
    return res; 
}
spiralOrder([])
console.log(spiralOrder([[1,2,3], [4, 5, 6], [7, 8, 9]]));